package cmich.cps.svn.hooks;

import java.util.Properties;
import java.util.logging.Logger;

import javax.mail.Address;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
/**
 * Sends an email.
 * 
 * @author Jonathan Kissinger <kissi1j@cmich.edu>
 */
public class SendEmail {
	private String to;
	private String from;
	private String subject;
	private String body;
	private final static Logger LOGGER = Logger.getLogger(SendEmail.class.getName());

	public SendEmail(String to, String subject, String body) {
		super();
		this.to = to + "@cmich.edu";
		this.subject = subject;
		this.body = body;
		this.from = "noreply@cmich.edu";
	}

	/**
	 * Sets up the email and sends it with {@link Transport#send(Message)}
	 */
	public void send() {
		LOGGER.fine("Emailing of results initiated.");
		Properties prop = System.getProperties();
		Session session = Session.getDefaultInstance(prop);
		Message message = new MimeMessage(session);
		try {
			message.setFrom(new InternetAddress(this.from));
			message.addRecipient(Message.RecipientType.TO, new InternetAddress(this.to));
			message.setSubject(this.subject);
			message.setText(this.body);
			Transport.send(message);
		} catch (MessagingException e) {
			LOGGER.warning(e.toString());
		}
		try {
			for(Address to : message.getAllRecipients()) {
				LOGGER.fine("Sending results email to: " + to.toString());
			}
		} catch (MessagingException e) {
			LOGGER.warning(e.toString());
		}
	}
}